import openpyxl
import os

def tomcat_log_collector(log_path):
    projects = os.listdir(log_path)
    project_dict = {}
    for project in projects:
        project_list = []
        project_dir = os.path.join(log_path, project)
        project_logs = os.listdir(project_dir)
        for project_log in project_logs:
            if 'tomcat' in project_log:
                project_log_path = os.path.join(project_dir, project_log)
                project_list.append(project_log_path)
        project_dict[project] = project_list
    copy_dict = project_dict.copy()
    for project in copy_dict:
        if not project_dict[project]:
            project_dict.pop(project)

    print(len(project_dict))
    return project_dict


# with open('AMIS_tomcat_amis-webroot-110000.log', 'r', encoding='utf-8') as f:
#     lines = f.readlines()
# data = {}
# keywords = ['服务器版本', 'cpu核心数', 'cpu使用率', '内存容量/内存使用率', '磁盘使用率', '系统日志', '内核参数',
#             '防火墙配置', '服务器总连接数', '服务器连接信息', 'tomcat进程状态', 'tomcat进程信息', 'Tomcat版本信息',
#             'Java版本信息', '进程jvm配置信息', '进程ulimit配置', '进程开机启动配置', 'Tomcat连接数', 'Tomcat连接信息',
#             'Tomcat配置文件列表', 'Tomcat配置文件内容:', '异常日志:']
# for index in range(len(keywords)):
#     start_keyword = keywords[index]
#     end_keyword = ''
#     if index != len(keywords) - 1:
#         end_keyword = keywords[index + 1]
#     # print(start_keyword, '---', end_keyword)
#
#     result = []
#     for line in lines:
#         if line.startswith(start_keyword):
#             if result:
#                 result[-1] += line
#             else:
#                 result.append(line)
#         elif end_keyword and line.startswith(end_keyword):
#             break
#         else:
#             if result:
#                 result[-1] += line
#     data[start_keyword] = result[0].replace('\n', '\\n')
#
# excel_filename = 'test.xlsx'
# sheet_name = 'Sheet1'
# wb = openpyxl.Workbook()
# ws = wb.active
# row_index = 0
# column_index = 0
#
# j = 0
# row_index += 1
# for data_keyword in keywords:
#     j += 1
#     ws.cell(row=row_index, column=j).value = data_keyword
#
# j = 0
# row_index += 1
# for project in data:
#     j += 1
#     ws.cell(row=row_index, column=j).value = str(data[project])
# wb.save(excel_filename)


if __name__ == '__main__':
    log_path = r'C:\Users\lixiu\OneDrive\桌面\2024第三季度'
    project_dict = tomcat_log_collector(log_path)

